<?php
declare (strict_types=1);

namespace app\admin\validates;

use think\Validate;

/**
 * Admin验证器
 */
class AdminValidate extends Validate
{
    /**
     * 验证规则
     * @var array
     */
    protected $rule = [
        'id' => 'require|number',
        'duties_id' => 'require|number',
        'name' => 'chsDash|max:15',
        'phone' => 'require',
        'mobile' => 'checkMobiles',
        'password' => 'require|max:32|alphaNum',
        'desc' => 'chsDash|max:255',
        'special_id' => 'require|number',
    ];

    /**
     * 验证字段描述
     * @var string[]
     */
    protected $field = [
        'id' => 'ID',
        'name' => '名称',
        'phone' => '工号',
        'password' => '密码',
        'desc' => '描述',
        'special_id' => '专业',
        'duties_id' => '职务',
        'mobile' => '手机号码',
    ];

    /**
     * 验证场景
     * @var array
     */
    protected $scene = [
        'add' => ['name', 'phone', 'password', 'special_id', 'desc','mobile','duties_id'],
        'edit' => ['id', 'name', 'phone', 'special_id' ,'desc','mobile','duties_id'],
        'del' => ['id'],
    ];

    /**
     * 验证手机号格式
     * @param string $value
     * @param string $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkMobile(string $value, string $rule, array $data)
    {
        if (isset($data['area_code']) && $data['area_code'] === '+86') {
            $preg = '/^1[3-9]\d{9}$/';
            if (!preg_match($preg, $value)) {
                return '手机号码格式错误';
            }
        }
        return true;
    }

    /**
     * 验证手机号格式
     * @param string $value
     * @param string $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkMobiles(string $value, string $rule, array $data)
    {
        $preg = '/^1[3-9]\d{9}$/';
        if (!preg_match($preg, $value)) {
            return '手机号码格式错误';
        }
        return true;
    }


}